home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Modules / mathmodule.c < prev    next >
C/C++ Source or Header  |  1998-01-27  |  7KB  |  294 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Math module -- standard C math library functions, pi and e */
  33.  
  34. #include "Python.h"
  35.  
  36. #include "mymath.h"
  37.  
  38. #ifndef _MSC_VER
  39. #ifndef __STDC__
  40. extern double fmod Py_PROTO((double, double));
  41. extern double frexp Py_PROTO((double, int *));
  42. extern double ldexp Py_PROTO((double, int));
  43. extern double modf Py_PROTO((double, double *));
  44. #endif /* __STDC__ */
  45. #endif /* _MSC_VER */
  46.  
  47.  
  48. #include "protos/mathmodule_protos.h"
  49.  
  50. #ifdef i860
  51. /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
  52. #undef HUGE_VAL
  53. #endif
  54.  
  55. #ifdef HUGE_VAL
  56. #define CHECK(x) if (errno != 0) ; \
  57.     else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
  58.     else errno = ERANGE
  59. #else
  60. #define CHECK(x) /* Don't know how to check */
  61. #endif
  62.  
  63. static PyObject *
  64. math_error()
  65. {
  66.     if (errno == EDOM)
  67.         PyErr_SetString(PyExc_ValueError, "math domain error");
  68.     else if (errno == ERANGE)
  69.         PyErr_SetString(PyExc_OverflowError, "math range error");
  70.     else
  71.                 /* Unexpected math error */
  72.         PyErr_SetFromErrno(PyExc_ValueError);
  73.     return NULL;
  74. }
  75.  
  76. #ifdef __SASC
  77. static PyObject *math_post_f(double x)
  78. {
  79.     CHECK(x);
  80.     if(errno!=0) return math_error();
  81.     else return PyFloat_FromDouble(x);
  82. }
  83.  
  84. #define FUNC1(stubname,func) \
  85. static PyObject * stubname(PyObject *self, PyObject *args) { \
  86.     double x; if(!PyArg_Parse(args, "d", &x)) return NULL; \
  87.     errno=0; x=func(x); return math_post_f(x); }
  88.  
  89. #define FUNC2(stubname,func) \
  90. static PyObject * stubname(PyObject *self, PyObject *args) { \
  91.     double x, y; if(!PyArg_Parse(args, "(dd)", &x, &y)) return NULL; \
  92.     errno=0; x=func(x,y); return math_post_f(x); }
  93. #else /* !__SASC */
  94. static PyObject *
  95. math_1(args, func)
  96.     PyObject *args;
  97.     double (*func) Py_FPROTO((double));
  98. {
  99.     double x;
  100.     if (!  PyArg_Parse(args, "d", &x))
  101.         return NULL;
  102.     errno = 0;
  103.     PyFPE_START_PROTECT("in math_1", return 0)
  104.     x = (*func)(x);
  105.     PyFPE_END_PROTECT(x)
  106.     CHECK(x);
  107.     if (errno != 0)
  108.         return math_error();
  109.     else
  110.         return PyFloat_FromDouble(x);
  111. }
  112.  
  113. static PyObject *
  114. math_2(args, func)
  115.     PyObject *args;
  116.     double (*func) Py_FPROTO((double, double));
  117. {
  118.     double x, y;
  119.     if (! PyArg_Parse(args, "(dd)", &x, &y))
  120.         return NULL;
  121.     errno = 0;
  122.     PyFPE_START_PROTECT("in math_2", return 0)
  123.     x = (*func)(x, y);
  124.     PyFPE_END_PROTECT(x)
  125.     CHECK(x);
  126.     if (errno != 0)
  127.         return math_error();
  128.     else
  129.         return PyFloat_FromDouble(x);
  130. }
  131.  
  132. #ifdef HAVE_PROTOTYPES
  133. #define FUNC1(stubname, func) \
  134.     static PyObject * stubname(PyObject *self, PyObject *args) { \
  135.         return math_1(args, func); \
  136.     }
  137.  
  138. #define FUNC2(stubname, func) \
  139.     static PyObject * stubname(PyObject *self, PyObject *args) { \
  140.         return math_2(args, func); \
  141.     }
  142. #else /* !HAVE_PROTOTYPES */
  143. #define FUNC1(stubname, func) \
  144.     static PyObject * stubname(self, args) PyObject *self, *args; { \
  145.         return math_1(args, func); \
  146.     }
  147.  
  148. #define FUNC2(stubname, func) \
  149.     static PyObject * stubname(self, args) PyObject *self, *args; { \
  150.         return math_2(args, func); \
  151.     }
  152. #endif
  153.  
  154. #endif /* !__SASC */
  155.  
  156. FUNC1(math_acos, acos)
  157. FUNC1(math_asin, asin)
  158. FUNC1(math_atan, atan)
  159. FUNC2(math_atan2, atan2)
  160. FUNC1(math_ceil, ceil)
  161. FUNC1(math_cos, cos)
  162. FUNC1(math_cosh, cosh)
  163. FUNC1(math_exp, exp)
  164. FUNC1(math_fabs, fabs)
  165. FUNC1(math_floor, floor)
  166. FUNC2(math_fmod, fmod)
  167. FUNC2(math_hypot, hypot)
  168. FUNC1(math_log, log)
  169. FUNC1(math_log10, log10)
  170. #ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
  171. FUNC2(math_pow, power)
  172. #else
  173. FUNC2(math_pow, pow)
  174. #endif
  175. FUNC1(math_sin, sin)
  176. FUNC1(math_sinh, sinh)
  177. FUNC1(math_sqrt, sqrt)
  178. FUNC1(math_tan, tan)
  179. FUNC1(math_tanh, tanh)
  180.  
  181.  
  182. static PyObject *
  183. math_frexp(self, args)
  184.     PyObject *self;
  185.     PyObject *args;
  186. {
  187.     double x;
  188.     int i;
  189.     if (! PyArg_Parse(args, "d", &x))
  190.         return NULL;
  191.     errno = 0;
  192.     x = frexp(x, &i);
  193.     CHECK(x);
  194.     if (errno != 0)
  195.         return math_error();
  196.     return Py_BuildValue("(di)", x, i);
  197. }
  198.  
  199. static PyObject *
  200. math_ldexp(self, args)
  201.     PyObject *self;
  202.     PyObject *args;
  203. {
  204.     double x, y;
  205.     /* Cheat -- allow float as second argument */
  206.         if (! PyArg_Parse(args, "(dd)", &x, &y))
  207.         return NULL;
  208.     errno = 0;
  209.     PyFPE_START_PROTECT("ldexp", return 0)
  210.     x = ldexp(x, (int)y);
  211.     PyFPE_END_PROTECT(x)
  212.     CHECK(x);
  213.     if (errno != 0)
  214.         return math_error();
  215.     else
  216.         return PyFloat_FromDouble(x);
  217. }
  218.  
  219. static PyObject *
  220. math_modf(self, args)
  221.     PyObject *self;
  222.     PyObject *args;
  223. {
  224.     double x, y;
  225.     if (! PyArg_Parse(args, "d", &x))
  226.         return NULL;
  227.     errno = 0;
  228. #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
  229. {
  230.     extended e;
  231.     x = modf(x, &e);
  232.     y = e;
  233. }
  234. #else
  235.     x = modf(x, &y);
  236. #endif
  237.     CHECK(x);
  238.     if (errno != 0)
  239.         return math_error();
  240.     return Py_BuildValue("(dd)", x, y);
  241. }
  242.  
  243. static PyMethodDef math_methods[] = {
  244.     {"acos", math_acos},
  245.     {"asin", math_asin},
  246.     {"atan", math_atan},
  247.     {"atan2", math_atan2},
  248.     {"ceil", math_ceil},
  249.     {"cos", math_cos},
  250.     {"cosh", math_cosh},
  251.     {"exp", math_exp},
  252.     {"fabs", math_fabs},
  253.     {"floor", math_floor},
  254.     {"fmod", math_fmod},
  255.     {"frexp", math_frexp},
  256.     {"hypot", math_hypot},
  257.     {"ldexp", math_ldexp},
  258.     {"log", math_log},
  259.     {"log10", math_log10},
  260.     {"modf", math_modf},
  261.     {"pow", math_pow},
  262.     {"sin", math_sin},
  263.     {"sinh", math_sinh},
  264.     {"sqrt", math_sqrt},
  265.     {"tan", math_tan},
  266.     {"tanh", math_tanh},
  267.     {NULL,        NULL}        /* sentinel */
  268. };
  269.  
  270. void
  271. initmath()
  272. {
  273.     PyObject *m, *d, *v;
  274.     
  275.     m = Py_InitModule("math", math_methods);
  276.     d = PyModule_GetDict(m);
  277.  
  278.         if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
  279.                 goto finally;
  280.     if (PyDict_SetItemString(d, "pi", v) < 0)
  281.                 goto finally;
  282.     Py_DECREF(v);
  283.  
  284.         if (!(v = PyFloat_FromDouble(exp(1.0))))
  285.                 goto finally;
  286.     if (PyDict_SetItemString(d, "e", v) < 0)
  287.                 goto finally;
  288.     Py_DECREF(v);
  289.     return;
  290.  
  291.   finally:
  292.         Py_FatalError("can't initialize math module");
  293. }
  294.